package com.jyy.prefabricated.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jyy.prefabricated.common.AjaxResult;
import com.jyy.prefabricated.dto.BatchDto;
import com.jyy.prefabricated.dto.Pageable;
import com.jyy.prefabricated.entity.ExamPaper;
import com.jyy.prefabricated.entity.SysUser;
import com.jyy.prefabricated.enums.BaseStatusEnum;
import com.jyy.prefabricated.enums.DeletedEnum;
import com.jyy.prefabricated.enums.PaperEnum;
import com.jyy.prefabricated.service.ExamPaperService;
import com.jyy.prefabricated.utils.UserSessionUtil;
import com.jyy.prefabricated.utils.UserUtils;
import com.jyy.prefabricated.vo.PracticeVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.awt.print.Paper;
import java.util.List;


/**
 * 考试试卷
 * @version 1.0.0
 * @author: zjj
 */
@Slf4j
@RequestMapping("/api")
@RestController
public class ExamPaperController {

    @Autowired
    private ExamPaperService paperService;

    @Resource
    private UserUtils userUtils;

    /**
     * 新增试卷/练习
     * @param paper
     * @return
     */
    @PostMapping("/exam-paper")
    public AjaxResult insert(@Valid @RequestBody ExamPaper paper){
        log.info("Rest request to insert exam-paper:{}",paper);
        paper.setStatus(BaseStatusEnum.INVALID.getValue());
        paper.setDeleted(DeletedEnum.INVALID.getValue());
        paper.setPaperStatus(PaperEnum.EXAM.getValue());
        int id = paperService.insert(paper);
        ExamPaper result = paperService.selectById(id);
        return AjaxResult.success(result);
    }

    /**
     * 修改试卷
     * @param paper
     * @return
     */
    @PutMapping("/exam-paper")
    public AjaxResult update(@Valid @RequestBody ExamPaper paper){
        log.info("Rest request to update exam-paper:{}",paper);
        paperService.update(paper);
        ExamPaper result = paperService.selectById(paper.getId());
        return AjaxResult.success(result);
    }

    /**
     * 查询试卷详情
     * @param id
     * @return
     */
    @GetMapping("/exam-paper/{id}")
    public AjaxResult selectById(@PathVariable Integer id){
        log.info("Rest request to find exam-paper by id:{}",id);
        ExamPaper result = paperService.selectById(id);
        return AjaxResult.success(result);
    }

    /**
     * 删除试卷
     * @param id
     * @return
     */
    @DeleteMapping("/exam-paper/{id}")
    public AjaxResult deleteById(@PathVariable Integer id){
        log.info("Rest request to delete exam-paper by id:{}",id);
        int row = paperService.delete(id);
        if(row > 0){
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
    }
    /**
     * 批量删除练习
     * @param dto
     * @return
     */
    @DeleteMapping("/exam-paper-batch")
    public AjaxResult deleteById(@RequestBody BatchDto dto){
        log.info("Rest request to batch-delete course by id:{}",dto);
        int row = paperService.batchDelete(dto);
        if(row > 0){
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
    }
    /**
     * 批量更新发布状态
     * @param dto
     * @return
     */
    @PutMapping("/exam-paper-batch")
    public AjaxResult update(@Valid @RequestBody BatchDto dto){
        log.info("Rest request to batch-update course:{}",dto);
        paperService.batchUpdateStatus(dto);
        return AjaxResult.success();
    }
    /**
     * 分页查询试卷列表
     * @param name 试卷名称
     * @param status    状态
     * @param pageable 分页信息
     * @return
     */
    @GetMapping("/exam-paper")
    public AjaxResult selectByParams(@RequestParam(required = false, defaultValue = "") String name, @RequestParam(required = false) Integer status, Pageable pageable){
        log.info("Rest request to find exam-paper  by params name:{},status:{}", name, status);
        SysUser user = null;
        Integer trainingOrganizationId = null;
        String token = UserSessionUtil.resolveToken();
        if (StringUtils.isNotBlank(token)) {
            user = userUtils.getCurrentUser();
        }
        if (user != null) {
            if (user.getTrainingOrganization() != null) {
                trainingOrganizationId = user.getTrainingOrganization().getId();
            }
        }
        PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<PracticeVo> papers = paperService.selectByParams(name, status,PaperEnum.EXAM.getValue() ,trainingOrganizationId);
        PageInfo<PracticeVo> result = new PageInfo<>(papers);
        return AjaxResult.success(result);
    }

    /**
     * 查询试卷列表
     * @param name 试卷名称
     * @param status    状态
     * @return
     */
    @GetMapping("/exam-paper-list")
    public AjaxResult selectByParams(@RequestParam(required = false, defaultValue = "") String name, @RequestParam(required = false) Integer status){
        log.info("Rest request to find exam-paper  by params name:{},status:{}", name, status);
        SysUser user = null;
        Integer trainingOrganizationId = null;
        String token = UserSessionUtil.resolveToken();
        if (StringUtils.isNotBlank(token)) {
            user = userUtils.getCurrentUser();
        }
        if (user != null) {
            if (user.getTrainingOrganization() != null) {
                trainingOrganizationId = user.getTrainingOrganization().getId();
            }
        }
        List<PracticeVo> result = paperService.selectByParams(name, status,PaperEnum.EXAM.getValue() ,trainingOrganizationId);
        return AjaxResult.success(result);
    }


}
